package com.wordLearning.word.domain.service.impl;


import com.wordLearning.word.common.entity.PageResult;
import com.wordLearning.word.common.enums.IsDeletedFlagEnum;
import com.wordLearning.word.domain.convert.WordInfoConverter;
import com.wordLearning.word.domain.entity.WordBO;
import com.wordLearning.word.domain.service.WordInfodomainService;
import com.wordLearning.word.infra.basic.entity.FavoriteWord;
import com.wordLearning.word.infra.basic.entity.UnmasteredWord;
import com.wordLearning.word.infra.basic.entity.Word;
import com.wordLearning.word.infra.basic.service.FavoriteWordService;
import com.wordLearning.word.infra.basic.service.LearningRecordService;
import com.wordLearning.word.infra.basic.service.UnmasteredWordService;
import com.wordLearning.word.infra.basic.service.WordService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class WordInfodomainServiceImpl implements WordInfodomainService {

    @Resource
    private WordService wordService;

    @Resource
    private FavoriteWordService favoriteWordService;

    @Resource
    private UnmasteredWordService unmasteredWordService;

    @Resource
    private LearningRecordService learningRecordService;

    @Override
    public void add(WordBO wordBO) {
        Word word = WordInfoConverter.INSTANCE.convertBoToEntity(wordBO);
        word.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        wordService.insert(word);
    }

    @Override
    public Boolean delete(WordBO wordBO) {
        Word word = WordInfoConverter.INSTANCE.convertBoToEntity(wordBO);
        word.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());
        int count = wordService.update(word);
        return count > 0;
    }

    @Override
    public Boolean update(WordBO wordBO) {
        Word word = WordInfoConverter.INSTANCE.convertBoToEntity(wordBO);
        int count = wordService.update(word);
        return count > 0;
    }

    @Override
    public PageResult<WordBO> getWordPage(WordBO wordBO) {
        PageResult<WordBO> pageResult = new PageResult<>();
        pageResult.setPageNo(wordBO.getPageNo());
        pageResult.setPageSize(wordBO.getPageSize());
        int start = (wordBO.getPageNo() - 1) * wordBO.getPageSize();
        Word word = WordInfoConverter.INSTANCE.convertBoToEntity(wordBO);
        int count = wordService.countByCondition(word);
        if(count == 0){
            return pageResult;
        }
        List<Word> wordList = wordService.queryPage(word,start,wordBO.getPageSize());
        List<WordBO> boList = WordInfoConverter.INSTANCE.convertEntityListToBOList(wordList);
        pageResult.setResult(boList);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Override
    public PageResult<FavoriteWord> getFavoritePage(WordBO wordBO) {
        PageResult<FavoriteWord> pageResult = new PageResult<>();
        pageResult.setPageNo(wordBO.getPageNo());
        pageResult.setPageSize(wordBO.getPageSize());
        int start = (wordBO.getPageNo() - 1) * wordBO.getPageSize();
        FavoriteWord favoriteWord = new FavoriteWord();
        favoriteWord.setWordId(wordBO.getWordId());
        int count = favoriteWordService.countByCondition(favoriteWord);
        if(count == 0){
            return pageResult;
        }
        List<FavoriteWord> wordList = favoriteWordService.queryPage(favoriteWord,start,wordBO.getPageSize());
        pageResult.setResult(wordList);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Override
    public PageResult<UnmasteredWord> getUnmasterPage(WordBO wordBO) {
        PageResult<UnmasteredWord> pageResult = new PageResult<>();
        pageResult.setPageNo(wordBO.getPageNo());
        pageResult.setPageSize(wordBO.getPageSize());
        int start = (wordBO.getPageNo() - 1) * wordBO.getPageSize();
        UnmasteredWord unmasteredWord = new UnmasteredWord();
        unmasteredWord.setWordId(wordBO.getWordId());
        int count = unmasteredWordService.countByCondition(unmasteredWord);
        if(count == 0){
            return pageResult;
        }
        List<UnmasteredWord> wordList = unmasteredWordService.queryPage(unmasteredWord,start,wordBO.getPageSize());
        pageResult.setResult(wordList);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Override
    public Integer countLearningRecord() {
        return learningRecordService.countByCondition();
    }

    @Override
    public Integer countUnmasterWord() {
        return unmasteredWordService.countByCondition(new UnmasteredWord());
    }

    @Override
    public WordBO queryWordBySpelling(WordBO wordBO) {
         Word word = WordInfoConverter.INSTANCE.convertBoToEntity(wordBO);
         Word resWord = wordService.queryWordBySpelling(word.getSpelling());
         return WordInfoConverter.INSTANCE.convertEntityToBO(resWord);
    }

    @Override
    public WordBO queryWordById(WordBO wordBO) {
        Word word = WordInfoConverter.INSTANCE.convertBoToEntity(wordBO);
        Word resWord = wordService.queryWordById(word.getWordId());
        return WordInfoConverter.INSTANCE.convertEntityToBO(resWord);
    }
}
